import socket,time  
  
def tcp_client(host, port, message):  
    # 创建一个socket对象  
    # socket.AF_INET表示IPv4地址，socket.SOCK_STREAM表示TCP协议  
    while True:
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
        try:  
            # 连接到服务器  
            client_socket.connect((host, port))  
    
            # 发送数据  
            while True:
                try: 
                    client_socket.sendall(message.encode('utf-8'))  
                    time.sleep(1)
                except:
                    print("SENDfail!")
                    # time.sleep(1)
                    client_socket.close() 
                    break
    
            # # 接收小于 1024 字节的数据  
            # data = client_socket.recv(1024)  
    
            # print('Received from server:', data.decode('utf-8'))  
        except:
            client_socket.close() 
            print("fail!")
        finally:  
            # 关闭socket  
            client_socket.close()  
        time.sleep(3)

if __name__ == '__main__':  
    HOST = '10.0.0.170'  # 服务器的IP地址  
    PORT = 5000        # 服务器监听的端口  
    MESSAGE = 'Hello, server,I am 3568_1!'  # 要发送给服务器的消息  
  
    tcp_client(HOST, PORT, MESSAGE)